package com.xysl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.xysl.api.domain.UserCustomizeOption;
import com.xysl.api.domain.UserCustomizeValue;
import com.xysl.constant.XyslConstants;
import com.xysl.domain.dto.QueryUserCustomizeDTO;
import com.xysl.domain.vo.UserCustomizeVo;
import com.xysl.mapper.UserCustomizeOptionMapper;
import com.xysl.mapper.UserCustomizeValueMapper;
import com.xysl.service.IUserCustomizeOptionService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户自定义配置类型Service接口
 *
 * @author xysl
 * @date 2025-01-23
 */
@Service
public class UserCustomizeOptionServiceImpl implements IUserCustomizeOptionService
{

    @Resource
    UserCustomizeOptionMapper optionMapper;
    @Resource
    UserCustomizeValueMapper valueMapper;

    @Override
    public UserCustomizeOption selectUserCustomizeOptionById(Long id) {
        return null;
    }

    @Override
    public List<UserCustomizeVo> selectUserCustomizeList(QueryUserCustomizeDTO param) {
        List<UserCustomizeVo> res = null;
        LambdaQueryWrapper<UserCustomizeOption> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getCustomizeType())) {
            wrapper.in(UserCustomizeOption::getCustomizeType,param.getCustomizeType().split(","));
        }
        wrapper.eq(UserCustomizeOption::getStatus,0);
        List<UserCustomizeOption> optionList = optionMapper.selectList(wrapper);

        if (!Collections.isEmpty(optionList)) {
            res = BeanUtils.converList(optionList,UserCustomizeVo.class);
            if (StringUtils.isNotNull(param.getUserId())) {
                LambdaQueryWrapper<UserCustomizeValue> valueWr = new LambdaQueryWrapper<>();
                valueWr.eq(UserCustomizeValue::getUserId,param.getUserId());
                if (StringUtils.isNotEmpty(param.getCustomizeType())) {
                    valueWr.in(UserCustomizeValue::getCustomizeType,param.getCustomizeType().split(","));
                }
                List<UserCustomizeValue> valueList = valueMapper.selectList(valueWr);
                if (!Collections.isEmpty(valueList)) {
                    Map<Long, UserCustomizeValue> valueMap = new HashMap<>();
                    for (UserCustomizeValue v : valueList) {
                        valueMap.put(v.getCustomizeOptionId(), v);
                    }
                    for (UserCustomizeVo o : res) {
                        UserCustomizeValue value = valueMap.get(o.getId());
                        o.setValueSort(99);
                        o.setIsSelect("0");
                        if (value != null) {
                            o.setIsSelect("1");
                            o.setValueSort(value.getSort());
                        }
                    }
                }
            }
            res.sort(Comparator.comparing(UserCustomizeVo::getCustomizeType)
                    .thenComparingInt(UserCustomizeVo::getValueSort)
                    .thenComparing(UserCustomizeVo::getIsDefault)
                    .thenComparingInt(UserCustomizeVo::getCustomizeSort));
        }
        return res;
    }

    @Override
    @Transactional
    public R userCustomizeConfig(QueryUserCustomizeDTO param) {
        if (StringUtils.isEmpty(param.getCustomizeType())) {
            return R.fail("更新配置类型不合规");
        }
        if (!Collections.isEmpty(param.getUpdateConfig())) {

            Object maxNum = XyslConstants.getConfigValue(param.getCustomizeType());
            //删除原来配置
            LambdaQueryWrapper<UserCustomizeValue> deWr = new LambdaQueryWrapper<>();
            deWr.eq(UserCustomizeValue::getUserId,param.getUserId());
            deWr.eq(UserCustomizeValue::getCustomizeType,param.getCustomizeType());
            valueMapper.delete(deWr);
            //保存新的配置 todo
            if (null != maxNum) {
                int maxNumInt = Integer.valueOf(maxNum.toString());
                if (!Collections.isEmpty(param.getUpdateConfig())) {
                }
            }
            for (String s : param.getUpdateConfig()) {
                UserCustomizeValue value = new UserCustomizeValue();
                value.setUserId(param.getUserId());
                value.setCustomizeType(param.getCustomizeType());
                value.setCustomizeOptionId(Long.valueOf(s.split(":")[0]));
                value.setSort(Integer.valueOf(s.split(":")[1]));
                value.setValue("1");
                valueMapper.insert(value);
            }
        }else {
            return R.fail("更新配置选项不合规");
        }
        return R.ok();
    }



    @Override
    public int insertUserCustomizeOption(UserCustomizeOption userCustomizeOption) {
        return 0;
    }

    @Override
    public int updateUserCustomizeOption(UserCustomizeOption userCustomizeOption) {
        return 0;
    }

    @Override
    public int deleteUserCustomizeOptionByIds(Long[] ids) {
        return 0;
    }

    @Override
    public int deleteUserCustomizeOptionById(Long id) {
        return 0;
    }
}
